package controller

import (
	"context"
	"github.com/RaymondCode/simple-demo/config"
	"github.com/RaymondCode/simple-demo/entity/myerr"
	"github.com/RaymondCode/simple-demo/entity/param"
	"github.com/RaymondCode/simple-demo/entity/response"
	"github.com/RaymondCode/simple-demo/middleware"
	"github.com/RaymondCode/simple-demo/service/impl"
	"github.com/RaymondCode/simple-demo/util/ossutil"
	"github.com/RaymondCode/simple-demo/util/webutil"
	"github.com/cloudwego/hertz/pkg/app"
	"log"
	"net/http"
	"strconv"
	"time"
)

// Feed 刷视频
func Feed(ctx context.Context, c *app.RequestContext) {
	// 处理截止时间
	var latestTime int64
	var timeParam = c.Query("latest_time")
	var err error
	videoService := serviceimpl.NewVideoServiceInstance()
	if timeParam == "" {
		latestTime = time.Now().UnixMilli()
	} else {
		latestTime, err = strconv.ParseInt(timeParam, 10, 64)
		if err != nil {
			log.Println(err)
			c.JSON(http.StatusInternalServerError, response.SystemError)
			return
		}
	}
	// 鉴权
	userId := 0
	isLogin := false
	if middleware.ThreadLocal.Get() != nil {
		isLogin = true
		userId, err = strconv.Atoi(middleware.ThreadLocal.Get().(map[string]string)[config.Config.ThreadLocal.Keys.UserId])
		if err != nil {
			log.Println(err)
			c.JSON(http.StatusInternalServerError, response.SystemError)
			return
		}
	}
	feed, nextTime, err := videoService.Feed(userId, isLogin, latestTime)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, response.SystemError)
		return
	}
	c.JSON(http.StatusOK, response.Feed{
		NextTime:  nextTime,
		VideoList: feed,
	})
}

// Publish 投稿视频
func Publish(ctx context.Context, c *app.RequestContext) {
	// 获取投稿人ID
	authorId, err := strconv.Atoi(middleware.ThreadLocal.Get().(map[string]string)[config.Config.ThreadLocal.Keys.UserId])
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, response.SystemError)
		return
	}
	// 获取视频标题
	var publishParam param.Publish
	err = c.BindAndValidate(&publishParam)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusBadRequest, response.ArgumentError(myerr.ArgumentInvalid(webutil.GetValidMsg(err, publishParam))))
		return
	}
	// 获取视频文件
	video, err := c.FormFile("data")
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusBadRequest, response.ErrorResponse(myerr.VideoNotFound))
		return
	}
	if !ossutil.IsVideo(video.Filename) {
		c.JSON(http.StatusBadRequest, response.ErrorResponse(myerr.FileError))
		return
	}
	err = serviceimpl.NewVideoServiceInstance().Publish(c, video, authorId, publishParam.Title)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusProxyAuthRequired, response.ErrorResponse(err))
		return
	}
	c.JSON(http.StatusOK, response.PubVideo{
		Response: response.Ok,
	})
}

// VideoList 查看视频发布列表
func VideoList(ctx context.Context, c *app.RequestContext) {
	var videoList param.VideoList
	var err error
	videoList.UserID, err = strconv.Atoi(c.Query("user_id"))
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusBadRequest, response.ArgumentError(myerr.ArgumentInvalid(webutil.GetValidMsg(err, videoList))))
		return
	}
	boVideos, err := serviceimpl.NewVideoServiceInstance().VideoList(videoList.UserID)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusBadRequest, response.ErrorResponse(err))
		return
	}
	c.JSON(http.StatusOK, response.VideoList{
		Response:  response.Ok,
		VideoList: boVideos,
	})
}
